package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.*;
import com.indexingsystem.boss.admin.mapper.IdxProductsMapper;
import com.indexingsystem.boss.admin.mapper.IdxTaskMapper;
import com.indexingsystem.boss.admin.mapper.IdxTaskProductMapper;
import com.indexingsystem.boss.common.BeanUtil;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxTaskProduct;
import com.indexingsystem.boss.utils.ArrayUtil;
import com.indexingsystem.system.service.IdxTaskProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
  * @Discription impl
  * @Author mineary
  * @Date 14:39 2018/10/31 
  * @Param 
  * @Return 
  */
@Service
public class IdxTaskProductServiceImpl implements IdxTaskProductService {

    @Resource
    private IdxTaskProductMapper idxTaskProductMapper;

    @Resource
    private IdxProductsMapper idxProductsMapper;

    @Resource
    private IdxTaskMapper idxTaskMapper;


    @Override
    public int deleteByPrimaryKey(String id) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(IdxTaskProduct record) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.insert(record);
    }

    @Override
    public int insertSelective(IdxTaskProduct record) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.insertSelective(record);
    }

    @Override
    public IdxTaskProduct selectByPrimaryKey(String id) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(IdxTaskProduct record) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(IdxTaskProduct record) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.updateByPrimaryKey(record);
    }

    @Override
    public Integer deleteByIdBatch(List<String> list) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.deleteByIdBatch(list);
    }

    @Override
    public List<IdxTaskProduct> selectByCondition(IdxTaskProductConditionVo idxTaskProductConditionVo) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.selectByCondition(idxTaskProductConditionVo.getIds());
    }

    @Override
    public List<IdxTaskProduct> selectProductsByPrimaryKey(String taskProductId) {
        // TODO Auto-generated method stub
        return idxTaskProductMapper.selectProductsByPrimaryKey(taskProductId);
    }
    //1
    @Override
    public PagedResult<RefIndexingResVo> getRefIndexingList(RefIndexingReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();
        List<String> ids = new ArrayList<>();
        String arrChildIdById = null;
        Integer topTaskId = getTopTaskIdByTaskId(reqVo.getTaskId());
        if (topTaskId == null) {
            return null;
        }

        arrChildIdById = idxTaskMapper.getArrchildidsByTaskId(topTaskId);
        ids = ArrayUtil.toArrayList(arrChildIdById.split(","));
        if (ids != null && ids.size() > 0) {
            if (!ids.contains(reqVo.getTaskId())) {
                ids.add(String.valueOf(reqVo.getTaskId()));
            }
        } else {
            ids.add(String.valueOf(reqVo.getTaskId()));
        }
        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.toPagedResult(idxTaskProductMapper.getRefIndexingListByTaskIds(ids));
    }

    //解决同步问题，修改测试。
    @Override
    public PagedResult<IdxProductsResVo> SubTaskUnAssignedProduct(TaskProductAddReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();
        List<String> ids = new ArrayList<>();
        List<String> productList = null;
        String arrChildIdById = null;

        //获取当前任务的父任务Id
        Integer parentId = idxTaskMapper.getParentIdByTaskId(reqVo.getTaskId());
        //获取父任务的所有子任务的id
        arrChildIdById = idxTaskMapper.getArrchildidsByTaskId(parentId);

        if (null != arrChildIdById && arrChildIdById != "") {
            ids = ArrayUtil.toArrayList(arrChildIdById.split(","));
            if (ids.contains(String.valueOf(parentId))) {
                ids.remove(String.valueOf(parentId));
            }
        }
        if (!ids.contains(String.valueOf(reqVo.getTaskId()))) {
            ids.add(String.valueOf(reqVo.getTaskId()));
        }
        productList = idxTaskProductMapper.SubTaskUnAssignedProduct(parentId, ids);

        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        if (null != productList && productList.size() > 0) {
            PageHelper.startPage(pageNo, pageSize);
            return BeanUtil.toPagedResult(idxProductsMapper.getProductListByIds(productList));
        } else {
            return null;
        }
    }

    /**
      * @Discription 更新子任务以及子任务的标引状态
      * @Author mineary
      * @Date 12:40 2018/10/26
      * @Param [ids, status]
      * @Return int
      */
    @Override
    public int updateRefIndexingByTaskIds(List<String> ids, Integer status) {
        return idxTaskProductMapper.updateRefIndexingByTaskIds(ids,status);
    }

    /**
     *
     * @param taskIds
     * @return
     */
    @Override
    public List<String> getProductsByTaskId(List<String> taskIds) {
        return idxTaskProductMapper.getProductIdByTaskId(taskIds);
    }

    //递归获取顶级任务Id
    private int getTopTaskIdByTaskId(int taskId) {
        Integer parentId = idxTaskMapper.getParentIdByTaskId(taskId);
        if (parentId == 0) {
            return taskId;
        } else {
            return getTopTaskIdByTaskId(parentId);
        }
    }


}
